﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Newtonsoft.Json;

namespace UFIDA.U8.YYNBJY.Operate
{
    public class TBMOMORDER
    {

        public static string TBMOM(string filename, string dbname, string cvouchid, bool ilog, bool iilog)
        {
            PUBMethod.PMethod.printmsgs(filename, "======================[" + DateTime.Now.ToString() + "]["+dbname+"][" + filename + "]方法START======================", true, ilog, iilog, System.Drawing.Color.Black);
            try
            {
                string condition = " and 1=1 ";
                if (!string.IsNullOrEmpty(cvouchid))
                {
                    condition += " and mo.MoCode='"+cvouchid+"' ";
                }
                string sql = @"select top 10 Status,(MoCode+'-'+cast(SortSeq as nvarchar)) as MoCode,md.MoDId,md.mdeptcode,InvCode,Qty,StartDate,DueDate,Remark,Define22 cInvName,RelsUser ,RelsDate from " + dbname+ @"..mom_order mo 
                                left join " + dbname + @"..mom_orderdetail md on mo.MoId = md.MoId
                                left join " + dbname + @"..mom_morder mom on md.MoDId = mom.MoDId
                                left join " + dbname + @"..Inventory INV on md.InvCode = INV.cInvCode
                                where 1 = 1 and Status = 3 and mo.createdate>='2024-12-01' and md.MoDId not in (select MODID from EF_LF_MESU8..MO_ORDER where DBNAME = '" + dbname + @"') " + condition+"";
                DataTable dt = DBHelp.SQLHelper.GetDataTable(sql);
                if (dt.Rows.Count>0)
                {
                    JSONDATA.MOMCODE jsm = new JSONDATA.MOMCODE();
                    jsm.GUID = Guid.NewGuid().ToString();
                    jsm.TRXID = "WoDownLoad";
                    List<JSONDATA.DATAItem> lid = new List<JSONDATA.DATAItem>();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        JSONDATA.DATAItem jda = new JSONDATA.DATAItem();
                        jda.WO_ID = dt.Rows[i]["MoCode"].ToString();
                        jda.SEQ= dt.Rows[i]["MoDId"].ToString();
                        jda.WO_TYPE_ID = "MPB";
                        jda.ITEM_ID= dt.Rows[i]["InvCode"].ToString();
                        jda.WO_QTY = dt.Rows[i]["Qty"].ToString();
                        jda.FACTORY_ID = dt.Rows[i]["mdeptcode"].ToString();
                        jda.PLAN_START_PROCESS_DATE= dt.Rows[i]["StartDate"].ToString();
                        jda.PLAN_COMP_DATE = dt.Rows[i]["DueDate"].ToString();
                        jda.CMT = dt.Rows[i]["Remark"].ToString();
                        jda.CUMSTOMER_ITEM_CMT = dt.Rows[i]["cInvName"].ToString();
                        jda.DUTY_USER_ID = dt.Rows[i]["RelsUser"].ToString();
                        jda.DUTY_DATE_TIME = dt.Rows[i]["RelsDate"].ToString();
                        lid.Add(jda);
                    }
                    jsm.DATA = lid;
                    string json = JsonConvert.SerializeObject(jsm);
                    PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "]调用生产订单参数:"+json, false, ilog, iilog, System.Drawing.Color.Red);
                    string result = PUBMethod.PMethod.CreatePostHttpResponses(Model.PUBCLASS.MESURL,json,null);
                    PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]返回参数:" + result, false, ilog, iilog, System.Drawing.Color.Red);
                    if (!string.IsNullOrEmpty(result))
                    {
                        if (result.Substring(0,5)!="false")
                        {
                            JSONDATA.RESJSON jre = JsonConvert.DeserializeObject<JSONDATA.RESJSON>(result);
                            string GUID = jre.GUID;
                            string RTNCODE = jre.RTNCODE;
                            string RTNMSG = jre.RTNMSG;
                            if (RTNCODE=="0")
                            {
                                List<string> lis = new List<string>();
                                PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "]生产订单同步成功:" + RTNMSG, true, ilog, iilog, System.Drawing.Color.Green);
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    string MoDId = dt.Rows[i]["MoDId"].ToString();
                                    string mocode = dt.Rows[i]["MoCode"].ToString();
                                    lis.Add("insert into EF_LF_MESU8..MO_ORDER(DBNAME,MOCODE,MODID,CREATETIME,DEFINE1)values('"+dbname+"','"+mocode+"','"+MoDId+"',GETDATE(),'"+GUID+"') ");
                                }
                                DBHelp.SQLHelper.ExecuteSqlTran(lis);
                            }
                            else
                            {
                                PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "]生产订单同步失败:" + RTNMSG, true, ilog, iilog, System.Drawing.Color.Red);
                            }
                        }
                        else
                        {
                            PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "]生产订单接口返回异常:"+result, true, ilog, iilog, System.Drawing.Color.Red);
                        }
                    }
                    else
                    {
                        PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "]生产订单接口返回空", true, ilog, iilog, System.Drawing.Color.Red);
                    }
                }
                else
                {
                    PUBMethod.PMethod.printmsgs(filename,"["+dbname+"]没有查到需要同步的生产订单", true, ilog, iilog, System.Drawing.Color.Red);
                }
            }
            catch (Exception ex)
            {
                PUBMethod.PMethod.printmsgs(filename, "["+filename+"]同步异常:" + ex.Message, true, ilog, iilog, System.Drawing.Color.Red);
            }
            PUBMethod.PMethod.printmsgs(filename, "======================[" + DateTime.Now.ToString() + "][" + dbname + "][" + filename+"]方法E N D======================", true, ilog, iilog, System.Drawing.Color.Black);
            return "";


        }


    }
}
